package net.sf.cldsql.sql2json.sql;

import java.util.ArrayList;
import java.util.Map;

import net.sf.cldsql.dj.utl.DiException;
import net.sf.cldsql.dj.utl.XfixParser;

/**
 * SqlQuery
 */
public class SqlRequestQuery extends SqlBaseQuery {
    @Override
    public StringBuilder buildSql() {
        StringBuilder sql = new StringBuilder();
        
        switch (stmtType) {
        case SELECT:
            buildSelect(sql, params);
            sql.append(" from " + tableName);
            buildWhere(sql, params);
            break;
        case UPDATE:
            sql.append("update from " + tableName);
            buildUpdateSet(sql, params);
            buildWhere(sql, params);
            break;
        case INSERT:
            sql.append("insert into " + tableName);
            buildInto(sql, params);
            buildValues(sql, params);
            break;
        case DELETE:
            sql.append("delete from " + tableName);
            buildWhere(sql, params);
            break;
        default:
            throw new DiException("Unknown sql statement type " + stmtType);
        }
        
        return sql;
    }
    
    public void parseRequest(Map<String,String[]> params) {
        for (String key : params.keySet()) {
            
            XfixParser prefix = new XfixParser(XfixParser.PREFIX, ":");
            XfixParser suffix = new XfixParser(XfixParser.SUFFIX, ":");
            
            String value = ((String[])params.get(key))[0];
            
            if (prefix.parse(key, "select") || prefix.parse(key, "s")) {
                stmtType = SELECT;
                String column = prefix.getOtherPart();
                if (value.isEmpty()) {
                    selectList.add (new AndEntry(column, "", value));
                } else {
                    selectList.add (new AndEntry(column, "as", value));
                }
                
            } else if (prefix.parse(key, "update") || prefix.parse(key, "u")) {
                stmtType = UPDATE;
                String column = prefix.getOtherPart();
                updateList.add (new AndEntry(column, "=", value));
            } else if (prefix.parse(key, "insert") || prefix.parse(key, "i")) {
                stmtType = INSERT;
                String column = prefix.getOtherPart();
                updateList.add (new AndEntry(column, "=", value));
            } else if (prefix.parse(key, "delete") || prefix.parse(key, "d")) {
                stmtType = DELETE;
            } else if (prefix.parse(key, "from") || prefix.parse(key, "f")) {
                tableName = prefix.getOtherPart();
            } else if (prefix.parse(key, "where") || prefix.parse(key, "w")) {

                String op = null;
                key = prefix.getOtherPart();
                
                if (suffix.parse(key, "eq")) {
                    op = "=";
                } else if (suffix.parse(key, "gt")) {
                    op = ">";
                } else if (suffix.parse(key, "gte")) {
                    op = ">=";
                } else if (suffix.parse(key, "lt")) {
                    op = "<";
                } else if (suffix.parse(key, "lte")) {
                    op = "<=";
                } else if (suffix.parse(key, "neq")) {
                    op = "!=";
                } else if (suffix.parse(key, "lik")) {
                    op = "like";
                } else if (suffix.parse(key, "lpr")) {
                    op = "(";
                } else if (suffix.parse(key, "rpr")) {
                    op = ")";
                } else if (suffix.parse(key, "and")) {
                    op = "and";
                } else if (suffix.parse(key, "or")) {
                    op = "or";
                } else if (suffix.parse(key, "in")) {
                    op = "in";
                }
                
                if (op != null) {
                    String column = suffix.getOtherPart();
                    whereList.add(new AndEntry (column, op, value)); 
                } else {
                    throw new DiException("unknown query map key: " + key); 
                }
                
            }
        }

    }

}
